public class test2 {
    public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode newNode = new ListNode();
        newNode.next=head;
        ListNode prev = newNode;
        ListNode cur = head;
        ListNode next = cur.next;
        ListNode nnext = next.next;
        while (cur!=null&&next!=null){
            cur.next=nnext;
            next.next=cur;
            prev.next=next;
            //交换完成,向后移
            prev=cur;
            cur=nnext;
            if(cur!=null) next=cur.next;
            if(next!=null) nnext=next.next;
        }
        return newNode.next;
    }


    public ListNode swapPairs1(ListNode head) {
        //递归
        if(head==null||head.next==null)
            return head;
        ListNode node1 = swapPairs(head.next.next);
        ListNode node2 = head.next;
        node2.next = head;
        head.next=node1;
        return node2;
    }
}
